class Contact < ActiveRecord::Base
  belongs_to :treatment
  belongs_to :regard
  belongs_to :user, :dependent => :destroy
  belongs_to :reference, :polymorphic => true
  has_many :profiles, :dependent => :destroy
  has_many :news_offered_materials

  validates_presence_of :email, :treatment_id, :regard_id
  validates_uniqueness_of :email
  #validates_uniqueness_of :first_name, :scope => :last_name, :case_sensitive => false

  VALID_REFERENCE_CLASS = [ "Publisher" ,"Institute", "Library", "LibraryDepartment", "Bookshop", "BookshopDepartment", "People"]

  LANGUAGES = ["es", "en"]
  MAIL_FORMAT = ["html", "text"]
  SEX = ["f", "m"]

  def is_spanish?
    language == "es"
  end

  def is_english?
    language == "en"
  end

  def self.search(search_last_name, reference_class, reference_text, page)
    items_per_page = count_record_per_page :contacts
    order = "last_name"

    conditions_clauses = []
    conditions_options = []

    if !search_last_name.nil? && search_last_name.size != 0
      conditions_clauses << "upper(last_name) like upper(?)"
      conditions_options << "%#{search_last_name}%"
    end

    if !reference_class.nil? && reference_class != "reference_all"
      conditions_clauses << "reference_type = ?"
      conditions_options << reference_class

      if reference_class == "Institute"
        joins = "LEFT OUTER JOIN institutes ON institutes.id = reference_id"
        conditions_clauses << "upper(institutes.name) like upper(?)"
        conditions_options << "%#{reference_text}%"
      end
      if reference_class == "Bookshop"
        joins = "LEFT OUTER JOIN bookshops ON bookshops.id = reference_id"
        conditions_clauses << "upper(bookshops.name) like upper(?)"
        conditions_options << "%#{reference_text}%"
      end      
      if reference_class == "Publisher"
        joins = "LEFT OUTER JOIN publishers ON publishers.id = reference_id"
        conditions_clauses << "upper(publishers.name) like upper(?)"
        conditions_options << "%#{reference_text}%"
      end
      if reference_class == "People"
        str_split = reference_text.split(",")
        last_name = ""
        first_name = ""
        
        if str_split.length > 0
          last_name = str_split[0].strip
        end
        if str_split.length > 1
          last_name = str_split[1].strip
        end        
        
        joins = "LEFT OUTER JOIN peoples ON peoples.id = reference_id"
        conditions_clauses << "upper(peoples.last_name) like upper(?)"
        conditions_clauses << "upper(peoples.first_name) like upper(?)"
        conditions_options << "%#{last_name}%"
        conditions_options << "%#{first_name}%"
      end
    end

    conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
    @contacts = Contact.paginate :per_page => items_per_page,:page => page, :joins => joins, :conditions => conditions, :order => order
    return @contacts
  end

  def has_profile_with_thematic?(thematic)
    if Profile.count(:conditions => ["contact_id = ? and reference_type = 'Thematic'", id]) == 0
      #si el contacto no tiene tematicas en su profile signigica que le gusta lo que venga
      return true
    else
      if !thematic.nil?
        #si el contacto tiene tematicas y el material tambien -> hay que buscar a ver si coinciden
        return Profile.count(:conditions => ["contact_id = ? and reference_type = 'Thematic' and reference_id = ?", id, thematic.id]) > 0
      else
        #si el contacto tiene alguna tematica en su profile y le estoy pasando un material que no tiene tematica -> asumo que no le va a servir el material (para no enviarle ba)
        return false
      end
    end
  end

  def self.all_has_profile_by_countries(countries)
    Profile.contacts_has_profile_by_countries(countries)
  end

end